package cn.sykj.car.service.impl;

import cn.sykj.car.common.Const;
import cn.sykj.car.entity.ConfigCardCategory;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.DictionaryDictException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.ConfigCardCategoryMapper;
import cn.sykj.car.service.IConfigCardCategoryService;
import cn.sykj.car.utils.DiffEntityUtils;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员卡类型配置 服务实现类
 * </p>
 *
 * @author duanzis
 * @since 2021-06-26
 */
@Service
@Slf4j
public class ConfigCardCategoryServiceImpl extends ServiceImpl<ConfigCardCategoryMapper, ConfigCardCategory> implements IConfigCardCategoryService {
    /**
     * 添加会员卡
     *
     * @param configCardCategory
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO addCardCategory(ConfigCardCategory configCardCategory) throws Exception {

        if(Utils.isEmpty(configCardCategory)){
            throw new ParameterInvalidException();
        }
        boolean ret = this.save(configCardCategory);

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("添加会员卡");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("添加会员卡，ID为：" + configCardCategory.getId() + ";" + "添加会员卡名称:" + configCardCategory.getCardName());
        return responseMessageVO;

    }
    /**
     * 删除会员卡
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO delete(Integer id) throws Exception {

        // 查询原始数据是否存在
        ConfigCardCategory configCardCategory = this.getById(id);
        if(Utils.isEmpty(configCardCategory)){
            throw new CommonException("","原始记录不存在，无需删除！");
        }
        boolean ret = this.removeById(id);

        // 构建操作日志参数
        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("删除会员卡");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("删除会员卡，ID为：" + configCardCategory.getId()+";"+"会员卡名称:"+configCardCategory.getCardName());
        return responseMessageVO;
    }

    /**
     * 修改地区
     *
     * @param configCardCategory
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO updateCardCategory(ConfigCardCategory configCardCategory) throws Exception {

        //判断原始记录是否存在
        ConfigCardCategory reg=this.getById(configCardCategory.getId());
        if(Utils.isEmpty(reg)||reg.getDelFlag().equals(Const.DEL_FLAG_TRUE)){
            throw new DictionaryDictException("","记录不存在");
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean ret= this.updateById(configCardCategory);
        // 定义修改字符串：
        String editMes = "";

        // 遍历修改的内容与原始内容相比较，如果有变更，则记录
        ConfigCardCategory cardCategory = this.getById(configCardCategory.getId());
        Map<String, List<Object>> diffMap = DiffEntityUtils.getDifferent(reg,cardCategory);
        if(Utils.notEmpty(diffMap) && diffMap.size() > 0){
            for(String s : diffMap.keySet()){
                editMes = editMes + ">" + s + "==>修改前【" + diffMap.get(s).get(0) + "】-修改后【" + diffMap.get(s).get(1) + "】;";
            }
        }

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("修改会员卡");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("修改会员卡，ID为：" + cardCategory.getId() + ";具体修改信息为：" + editMes);
        return responseMessageVO;
    }

}
